package com.mineapp.weight

/**
 * Created by cyril on 16/7/11.
 */

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * ----------Dragon be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神兽出没━━━━━━by:wangziren
 */


class LoadingView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var mPaint: Paint? = null
    private var mPaintCircle: Paint? = null
    private var mPaintBall: Paint? = null
    private var mPaintStrokeWidth: Float = 0.toFloat()
    private var mHeight = 0f
    private var mWidth = 0f
    private var quadToStart = 0f
    private var mRadius = 0f
    private var mRadiusBall = 0f
    private var ballY = 0f

    internal var path = Path()

    init {
        initPaint()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mHeight = measuredHeight.toFloat()
        mWidth = measuredWidth.toFloat()
        quadToStart = mHeight / 2
        mRadius = dip2px(3f).toFloat()
        mPaintStrokeWidth = 2f
        ballY = mHeight / 2
        mRadiusBall = dip2px(4f).toFloat()
    }

    fun dip2px(dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        path = Path()
        path.moveTo(0f + mRadius * 2 + mPaintStrokeWidth, (measuredHeight / 2).toFloat())
        path.quadTo(mWidth / 2, quadToStart, mWidth - mRadius * 2 - mPaintStrokeWidth, mHeight / 2)
        mPaint!!.strokeWidth = 2f
        canvas.drawPath(path, mPaint!!)

        mPaintCircle!!.strokeWidth = mPaintStrokeWidth
        canvas.drawCircle(mRadius + mPaintStrokeWidth, mHeight / 2, mRadius, mPaintCircle!!)
        canvas.drawCircle(mWidth - mRadius - mPaintStrokeWidth, mHeight / 2, mRadius, mPaintCircle!!)

        if (ballY - mRadiusBall > mRadiusBall) {
            canvas.drawCircle(mWidth / 2, ballY - mRadiusBall, mRadiusBall, mPaintBall!!)
        } else {
            canvas.drawCircle(mWidth / 2, mRadiusBall, mRadiusBall, mPaintBall!!)
        }
    }

    private fun initPaint() {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.color = Color.WHITE

        mPaintCircle = Paint()
        mPaintCircle!!.isAntiAlias = true
        mPaintCircle!!.style = Paint.Style.STROKE
        mPaintCircle!!.color = Color.WHITE

        mPaintBall = Paint()
        mPaintBall!!.isAntiAlias = true
        mPaintBall!!.style = Paint.Style.FILL
        mPaintBall!!.color = Color.WHITE
    }

    internal var valueAnimator: ValueAnimator? = null

    fun stopAnim() {
        if (valueAnimator != null) {
            clearAnimation()
            quadToStart = mHeight / 2
            ballY = mHeight / 2
            valueAnimator!!.repeatCount = 0
            valueAnimator!!.cancel()
            valueAnimator!!.end()
        }
    }

    private fun startViewAnim(startF: Float, endF: Float, time: Long): ValueAnimator? {
        valueAnimator = ValueAnimator.ofFloat(startF, endF)
        valueAnimator!!.duration = time
        valueAnimator!!.interpolator = LinearInterpolator()
        valueAnimator!!.repeatCount = ValueAnimator.INFINITE
        valueAnimator!!.repeatMode = ValueAnimator.REVERSE
        valueAnimator!!.addUpdateListener {
            val value = valueAnimator!!.animatedValue as Float
            if (value > 0.75) {
                quadToStart = mHeight / 2 - (1f - valueAnimator!!.animatedValue as Float) * mHeight / 3f
            } else {
                quadToStart = mHeight / 2 + (1f - valueAnimator!!.animatedValue as Float) * mHeight / 3f
            }
            if (value > 0.35f) {
                ballY = mHeight / 2 - mHeight / 2 * value
            } else {
                ballY = mHeight / 2 + mHeight / 6 * value
            }
            invalidate()
        }
        valueAnimator!!.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
            }
        })
        if (!valueAnimator!!.isRunning) {
            valueAnimator!!.start()
        }
        return valueAnimator
    }

    fun startAnim() {
        stopAnim()
        startViewAnim(0f, 1f, 800)
    }
}
